<script setup>
import {Plus} from "@element-plus/icons-vue";
import {httpUpload} from "@/api";

const props = defineProps({
  image: {
    type: String,
    default: ''
  }
})
const emits = defineEmits(['update:image'])

const uploadAvatar = (file) => {
  // 上传头像
  httpUpload(file.file).then(res => {
    emits('update:image', res.data.url)
  })
}
</script>

<template>
  <el-upload
      class="avatar-uploader"
      action="#"
      :http-request="uploadAvatar"
      :show-file-list="false"
      accept="image/*"
  >
    <el-image v-if="image" :src="image" class="avatar" />
    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
  </el-upload>
</template>

<style  lang="scss">
.avatar-uploader {
  width: 100%;
}
.avatar-uploader .avatar {
  width: 100%;
  height: 120px;
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}
.el-upload {
  width: 120px;
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100%;
  height: 120px;
  text-align: center;
}
</style>